import time
import numpy as np
import serial
import struct
ser=serial.Serial('/dev/ttyAMA0',115200)

def serialwws_init(btl):
    global ser
    ser=serial.Serial('/dev/ttyAMA0',btl)
    if ser.is_open:
        print("serial chushi ok")
        
def serialwws_close():
    ser.close()
def serialwws_send_xy(x,y):
    ser.write(str(x).encode())
    time.sleep(0.05)
    ser.write(str(y).encode())
def serialwws_send_bao(x,y,mubiaopan,yu=0):
    datap=b''
    datas=np.zeros(12,np.uint8)
    #start verify
    datas[0]=0xA5
    datas[1]=0x5A
    #datas
    #x
    if x>0:
        datas[2]=0
    else :
        datas[2]=1
        x=-x
    datas[3]=x/256
    datas[4]=x%256
    #y
    if y>0:
        datas[5]=0
    else :
        datas[5]=1
        y=-y
    datas[6]=y/256
    datas[7]=y%256
    #mubiao
    datas[8]=mubiaopan
    datas[9]=yu
    #checkout
    sumdatas=0
    for i in range(2,10):
        sumdatas+=datas[i]
    datas[10]=sumdatas & 0xFF
    #end
    datas[11]=0xFF
    #send
    for a in range(0,12):
        block=struct.pack('B',datas[a])
        datap+=block
    ser.write(datap)
    time.sleep(0.2)
    print("send ok")
    
    
